<!DOCTYPE html>
<html>
<head>
  <title>CP/Mega88 on NaCl</title>

  <script type="text/javascript"
      src="http://terminal-js.googlecode.com/git/term.js"></script>
  <script type="text/javascript"
      src="http://fsutils-js.googlecode.com/git/fsutils.js"></script>
  <script type="text/javascript">
    CpMega88Module = null;
    Terminal = null;
    File = null;
    var didLoad = false;
    var log = '';

    function moduleDidLoad() {
      Terminal.appendString('OK\n' + log);
      File = new FsUtils(false, 4 * 1024 * 1024, function(result) {
        if (!result)
          return;
        Terminal.appendString('Fetching files...\n  sdcard.img...'); 
        File.batch([
              { cmd: FsUtils.CMD_FETCH, name: 'sdcard.img', url: 'sdcard.img',
                callback: function(result) {
                  if (result)
                    Terminal.appendString('OK');
                  else
                    Terminal.appendString('ERROR');
                  Terminal.appendString('\n  eeprom.img...');
                } },
              { cmd: FsUtils.CMD_FETCH, name: 'eeprom.img', url: 'eeprom.img',
                callback: function(result) {
                  if (result)
                    Terminal.appendString('OK');
                  else
                    Terminal.appendString('ERROR');
                  Terminal.appendString('\n');
                } },
            ],
            function(result) {
              if (result) {
                Terminal.appendString('DONE\n\n');
                didLoad = true;
                document.getElementById('cpmega88').postMessage('B');
              } else {
                Terminal.appendString('ERROR\nHALT');
              }
            });
      });
    }

    function handleMessage(message_event) {
      var data = message_event.data;
      if (!data)
        return;
      var type = data[0];
      switch (type) {
      case 'E':
        console.log(data.slice(1));
        break;
      case 'S':
        var id = data[1];
        if (1 == id) {
          if (didLoad)
            Terminal.appendString(data.slice(2));
          else
            log += data.slice(2);
        } else {
          console.log(data.slice(2));
        }
      }
    }

  </script>
</head>
<body>

<h1>Native Client Module CP/Mega88</h1>
<p>
  <div id="listener">
    <pre id="term"
	 style="background-color: black;
		color: green;
		display: inline-block;"></pre>
    <script type="text/javascript">
      Terminal = new Term('term', 80, 25);
      Terminal.appendString('Loading NaCl module...');
      document.onkeypress = function (e) {
        document.getElementById('cpmega88').postMessage(
            'S0' + String.fromCharCode(e.which));
      }
      document.onkeydown = function (e) {
        if (8 == e.which) {
          document.getElementById('cpmega88').postMessage(
              'S0' + String.fromCharCode(e.which));
          return false;
        }
        return true;
      }
      document.onkeyup = function (e) {
        return false;
      }
      var listener = document.getElementById('listener');
      listener.addEventListener('load', moduleDidLoad, true);
      listener.addEventListener('message', handleMessage, true);
    </script>

    <embed name="nacl_module"
       id="cpmega88"
       width=0 height=0
       src="cpmega88.nmf"
       type="application/x-nacl" />
  </div>
</p>
</body>
</html>
